#include"SeqList.h"
#include<malloc.h>
#include<stdio.h>
#include<assert.h>
SeqList ps;
//初始化顺序表
void SeqListInit(SeqList* ps, int initcapacity) {
	assert(ps);
	ps->array = (DataType*)malloc((initcapacity) * (sizeof(DataType)));
	if (ps->array == NULL) {
		return;
	}
	ps->capacity = initcapacity;
	ps->size = 0;
}
//销毁顺序表
//void SeqListDestory(SeqList* ps) {
//	assert(ps);
//	if (ps->array)
//	{
//		free(ps->array);
//		ps->array = NULL;
//		ps->capacity = 0;
//		ps->size = 0;
//
//	}
//}
 void SeqListDestory(SeqList* ps) {
	assert(ps);
	if (ps->array)
	{
		ps->size = 0;

	}
}
//打印顺序表
void SeqListPrint(SeqList* ps) {
	assert(ps);
  int i=0;
	for (i = 0; i < ps->size; i++) {
		printf("%d ", ps->array[i]);
	}
	printf("\n");
}
//尾插
void SeqListPushBack(SeqList* ps, DataType x) {
	assert(ps);
	if (ps->capacity = ps->size) {
		SeqListExpand(ps);
	}
	ps->array[ps->size] = x;
	ps->size++;
}
//头插
void SeqListPushFront(SeqList* ps, DataType x) {
	assert(ps);
	if (ps->capacity == ps->size) {
		SeqListExpand(ps);
	}
  int i=0;
	for (i = ps->size; i >= 0 ; i--) {
		ps->array[i + 1] = ps->array[i];
	}
	ps->array[0] = x;
	ps->size++;
}
//头删
void SeqListPopFront(SeqList* ps) {
	assert(ps);
	if (ps->size == 0) {
		return;
	}
  int i=0;
	for (i = 0; i <= ps->size; i++) {
		ps->array[i - 1] = ps->array[i];
	}
	ps->size--;
}
//尾删
void SeqListPopBack(SeqList* ps) {
	assert(ps);
	if (ps->size == 0) {
		return;
	}
	ps->size--;
}
// 顺序表查找
int SeqListFind(SeqList* ps, DataType x) {
	assert(ps);
  int i=0;
	for ( i = 0; i < ps->size; i++) {
		if (ps->array[i] == x) {
			return i;
		}
	}
	return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, DataType x) {
	assert(ps);
	if (ps->capacity == ps->size) {
		SeqListExpand(ps);
	}
  int i=0;
	for ( i = ps->size; i >= pos; i--) {
		ps->array[i + 1] = ps->array[i];
	}
	ps->array[pos] = x;
	ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos) {
	assert(ps);
  int i=0;
	for ( i = pos+1; i <= ps->size; i++) {
		ps->array[i -1] = ps->array[i];
	}
	ps->size--;
}
//扩容
void SeqListExpand(SeqList* ps) {
	ps->capacity *= 2;
	ps->array = (DataType*)realloc(ps->array, ps->capacity * (sizeof(DataType)+1));
}


void Test() {
	SeqListInit(&ps,10);
	/*SeqListPushBack(&ps, 1);
	SeqListPushBack(&ps, 2);
	SeqListPushBack(&ps, 3);
	SeqListPushBack(&ps, 4);
	SeqListPushBack(&ps, 5);
	SeqListPushFront(&ps, 6);
	SeqListPushFront(&ps, 7); 
	SeqListPushFront(&ps, 8);
	SeqListPopFront(&ps);
	SeqListPopBack(&ps);
	SeqListInsert(&ps, SeqListFind(&ps,1), 9);
	SeqListErase(&ps,SeqListFind(&ps,1));
	SeqListPrint(&ps);
	SeqListDestory(&ps);*/
	SeqListPushBack(&ps, 1);
	SeqListPushBack(&ps, 2);
	SeqListPushBack(&ps, 3);
	SeqListPushBack(&ps, 4);
	SeqListPrint(&ps);
	SeqListPushFront(&ps, 6);
	SeqListPushFront(&ps, 7);
	SeqListPushFront(&ps, 8);
	SeqListPrint(&ps);
	SeqListPopFront(&ps);
	SeqListPrint(&ps);
	SeqListPopBack(&ps);
	SeqListPrint(&ps);
	SeqListInsert(&ps, SeqListFind(&ps, 1), 9);
	SeqListPrint(&ps);
	SeqListErase(&ps, SeqListFind(&ps, 1));
	SeqListPrint(&ps);
	SeqListDestory(&ps);
	
}
int main() {
	Test();
	return 0;
}

